<template>
  <div class="categories">
    <!-- 添加分类 -->
    <el-button type="success" plain class="add" @click="showCategoriesDailog">添加分类</el-button>
    <!-- 表格 -->
    <el-table
      v-loading="isLoading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255, 255, 255, 0.5)"
      :data="categoriesList"
      row-key="cat_id"
      :tree-props="{children: 'children'}"
    >
      <el-table-column label="分类名称" prop="cat_name"></el-table-column>
      <el-table-column label="是否有效">
        <template v-slot:default="{row}">{{row.cat_delete?'否':'是'}}</template>
      </el-table-column>
      <el-table-column label="层级" prop="cat_level"></el-table-column>
      <el-table-column label="操作">
        <template v-slot:default="{ row }">
          <!-- <p>{{row}}</p> -->
          <el-button type="primary" size="small" plain icon="el-icon-edit"></el-button>
          <el-button type="danger" size="small" plain icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 添加对话框 -->
    <el-dialog title="添加分类" :visible.sync="dialogVisible"  @close="closeDialog" width="40%">
      <el-form :model="addForm" :rules="rules" ref="addForm" label-width="80px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级名称">
          <el-cascader
              clearable
             v-model="addForm.cat_pid"
             :options="options"
             :props="props">
          </el-cascader>
        </el-form-item>
      </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategories">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created () {
    this.isLoading = true
    this.getCategories()
  },
  data () {
    return {
      dialogVisible: false,
      isLoading: false,
      categoriesList: [],
      pagenum: 1,
      pagesize: 4,
      total: 0,
      children: 'children',
      addForm: {
        cat_name: '', // 分类名称
        cat_pid: [], // 分类父ID
        cat_level: '' // 分类层级
      },
      options: [],
      props: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true // 来设置父子节点取消选中关联，从而达到选择任意一级选项的目的
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  methods: {
    // 渲染页面
    async getCategories () {
      const { meta, data } = await this.$axios.get(`categories`, {
        params: {
          type: 3,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      if (meta.status === 200) {
        this.$message.success(meta.msg)
        this.isLoading = false
        this.categoriesList = data.result
        this.total = data.total
      }
    },
    // 每页显示条数
    handleSizeChange (val) {
      this.pagesize = val
      this.pagenum = 1
      this.getCategories()
      // console.log(`每页 ${val} 条`)
    },
    // 当前页码
    handleCurrentChange (val) {
      this.pagenum = val
      this.getCategories()
      // console.log(`当前页: ${val}`)
    },
    // 添加对话框的显示
    async showCategoriesDailog () {
      try {
        this.dialogVisible = true
        //  只需要一二级的商品供给用户选择
        const { data, meta } = await this.$axios.get(`categories?type=2`)
        if (meta.status === 200) {
          this.$message.success(meta.msg)
          this.options = data
          // console.log(data)
        } else {
          this.$message.error(meta.msg)
        }
      } catch (e) {
        console.log(e)
      }
    },
    //  添加分类
    async  addCategories () {
      await this.$refs.addForm.validate()
      //  cat_pid  取到的是数组的最后一项
      const pidArr = this.addForm.cat_pid
      const catPid = pidArr[pidArr.length - 1] || 0
      const catName = this.addForm.cat_name
      const catLevel = pidArr.length
      const { meta } = await this.$axios.post(`categories`, {
        cat_pid: catPid,
        cat_name: catName,
        cat_level: catLevel })
      if (meta.status === 201) {
        this.dialogVisible = false
        this.$message.success(meta.msg)
        this.getCategories()
      }
    },
    // 关闭对话框重置表单
    closeDialog () {
      this.$refs.addForm.resetFields()
    }
  }
  // watch    () {
  //   addForm.cat_pid() {
  //     this.refs.addForm.e
  //   }
  // }
}
</script>

<style lang="scss" scoped>
.categories {
  .add {
    margin-bottom: 10px;
  }
}
</style>
